package com.twu.biblioteca;

public class Movie implements ShelfItem {

    private final int MAXIMUM_RATING = 10;
    private final int MINIMUM_RATING = 1;

    private String title;
    private String year;
    private String director;
    private Integer rating;
    private String DEFAULT_RATING = "N/A";
    private String SPACE = "\t";

    public Movie(String title, String year, String director, Integer rating) {
        this.title = title;
        this.year = year;
        this.director = director;

        setRating(rating);
    }

    public Movie(String title, String year, String director) {
        this.title = title;
        this.year = year;
        this.director = director;
    }

    public String getTitle() {
        return title;
    }

    public void setRating(Integer rating) {
        if (!isRatingValid(rating)) throw new IllegalArgumentException();
        this.rating = rating;
    }

    public Integer getRating() {
        return rating;
    }

    private boolean isRatingValid(Integer rating) {
        return rating >= MINIMUM_RATING && rating <= MAXIMUM_RATING;
    }

    @Override
    public String toString() {
        String rating = (this.rating == null) ? DEFAULT_RATING : this.rating.toString();
        return this.title + SPACE + this.year + SPACE + this.director + SPACE + rating;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Movie movie = (Movie) o;

        if (director != null ? !director.equals(movie.director) : movie.director != null) return false;
        if (rating != null ? !rating.equals(movie.rating) : movie.rating != null) return false;
        if (title != null ? !title.equals(movie.title) : movie.title != null) return false;
        if (year != null ? !year.equals(movie.year) : movie.year != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (year != null ? year.hashCode() : 0);
        result = 31 * result + (director != null ? director.hashCode() : 0);
        result = 31 * result + (rating != null ? rating.hashCode() : 0);
        return result;
    }
}
